package com.example.myproject.mapper;

import com.example.myproject.entity.UserEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Mybatis的接口
 * 不能有重名的方法
 */
//@Mapper
public interface UserMapper {

    // 批量查
    @Select("SELECT * FROM users")
    @Results({
            @Result(property = "userSex",  column = "user_sex", javaType = String.class),
            @Result(property = "nickName", column = "nick_name")
    })
    List<UserEntity> getAll();


    @Select("SELECT * FROM users WHERE id = #{id}")
    @Results({
            @Result(property = "userSex",  column = "user_sex", javaType = String.class),
            @Result(property = "nickName", column = "nick_name")
    })
    UserEntity getOneById(Long id);

    @Select("SELECT * FROM users WHERE user_sex = #{user_sex}")
    @Results({
            @Result(property = "userSex",  column = "user_sex", javaType = String.class),
            @Result(property = "nickName", column = "nick_name")
    })
    List<UserEntity> getBySex(String userSex);


    @Select("SELECT * FROM users WHERE cardNo = #{cardNo}")
    @Results({
            @Result(property = "userSex",  column = "user_sex", javaType = String.class),
            @Result(property = "nickName", column = "nick_name")
    })
    UserEntity getOne(String cardNo);

    @Insert("INSERT INTO users(cardNo,userName,passWord,user_sex,nick_name) VALUES(#{cardNo}, #{userName}, #{passWord}, #{userSex}, #{nickName})")
    void insert(UserEntity user);

    //@Update("UPDATE users SET cardNo=#{cardNo},userName=#{userName},passWord=#{passWord},user_sex=#{userSex},nick_name=#{nickName} WHERE id =#{id}")
    @Update("UPDATE users SET cardNo=#{cardNo},userName=#{userName},passWord=#{passWord},user_sex=#{userSex},nick_name=#{nickName} WHERE cardNo =#{cardNo}")
    void update(UserEntity user);

    @Delete("DELETE FROM users WHERE id =#{id}")
    void deleteById(Long id);

    @Delete("DELETE FROM users WHERE cardNo =#{cardNo}")
    void delete(String cardNo);

}
